import requests
import json


class BaiduTranslate(object):

    def __init__(self, kw):
        """
        初始化 域名 数据
        :param text: 输入要翻译的值
        """
        self.baidu_translate_url = "https://fanyi.baidu.com/sug"
        self.baidu_translat_data = {"kw": kw}
        self.baidu_translat_headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63"}

    def get_result(self):
        """
        发送请求
        :return: 返回请求结果
        """
        result = requests.post(self.baidu_translate_url, self.baidu_translat_data, self.baidu_translat_headers)
        return result.content.decode()

    def deal_with_result(self, data):
        """
        处理发送请求返回的结果
        :return: 处理之后的结果
        """
        dict_data = json.loads(data)
        print(dict_data['data'][0]['v'])

    def run(self):
        """
        发送请求
        :return: 将请求结果放入处理工具中
        """
        res = self.get_result()
        self.deal_with_result(res)


if __name__ == '__main__':
    word = input("请输入要翻译的单词或句子")
    baidu_translate = BaiduTranslate(word)
    baidu_translate.run()
